package com.thothsun.notificationdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.content.ContextCompat;

import com.thothsun.notificationdemo.handlers.BigTextIntentService;
import com.thothsun.notificationdemo.handlers.BigTextMainActivity;

public class MainActivity extends AppCompatActivity {

    public static final int NOTIFICATION_ID = 888;

    private NotificationManagerCompat notificationManagerCompat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());

        findViewById(R.id.btn_notice).setOnClickListener(v -> {
            if (!notificationManagerCompat.areNotificationsEnabled()) {
                Toast.makeText(this, "应用通知权限被禁用，请在设置页面打开权限", Toast.LENGTH_SHORT).show();
                openNotificationSettingsForApp();
                return;
            }
            generateNotification();
        });
    }

    private void generateNotification() {
        String notificationChannelId = NotificationUtils.createNotificationChannel(this, "channel_0", "资金通知", "xx", NotificationManager.IMPORTANCE_DEFAULT, false, NotificationCompat.VISIBILITY_PUBLIC);

        BigTextStyle bigTextStyle = new BigTextStyle().bigText("支付宝到账888888元").setBigContentTitle("动账通知").setSummaryText("入账");

        Intent notifyIntent = new Intent(this, BigTextMainActivity.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent dismissIntent = new Intent(this, BigTextIntentService.class);
        dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);

        PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
        NotificationCompat.Action dismissAction = new NotificationCompat.Action.Builder(R.drawable.ic_alarm, "I know", dismissPendingIntent).build();

        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
        GlobalNotificationBuilder.setNotificationCompatBuilder(notificationCompatBuilder);
        Notification notification = notificationCompatBuilder
                .setStyle(bigTextStyle)
                .setContentTitle("动账通知（low）")
                .setContentText("支付宝到账80000元")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_alarm))
                .setContentIntent(notifyPendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.design_default_color_primary))
                .setCategory(Notification.CATEGORY_REMINDER)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                //25及以前在这里设置，26及以后在channel里设置
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .addAction(dismissAction)
                .build();
        notificationManagerCompat.notify(NOTIFICATION_ID, notification);
    }

    private void openNotificationSettingsForApp() {
        Intent intent = new Intent();
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
        intent.putExtra("app_package", getPackageName());
        intent.putExtra("app_uid", getApplicationInfo().uid);
        intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
        startActivity(intent);
    }


}